package com.atguigu.tingshu.album.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.tingshu.album.mapper.*;
import com.atguigu.tingshu.album.service.BaseCategoryService;
import com.atguigu.tingshu.model.album.BaseCategory1;
import com.atguigu.tingshu.model.album.BaseCategoryView;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tencentcloudapi.hcm.v20181106.models.EvaluationRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

@Service
@SuppressWarnings({"all"})
public class BaseCategoryServiceImpl extends ServiceImpl<BaseCategory1Mapper, BaseCategory1> implements BaseCategoryService {

	@Autowired
	private BaseCategory1Mapper baseCategory1Mapper;

	@Autowired
	private BaseCategory2Mapper baseCategory2Mapper;

	@Autowired
	private BaseCategory3Mapper baseCategory3Mapper;


	@Autowired
	private BaseCategoryViewMapper baseCategoryViewMapper;

	@Autowired
	private BaseAttributeMapper baseAttributeMapper;


	@Override
	public List<JSONObject> getBaseCategoryList() {
		//JsonObject 拼接json数据
		List<JSONObject> list1 =new ArrayList<>();

		LambdaQueryWrapper<BaseCategoryView> wrapper=new LambdaQueryWrapper().select();

		List<BaseCategoryView> baseCategoryViews = baseCategoryViewMapper.selectList(wrapper);
//        为第一组分组
		Map<Long, List<BaseCategoryView>> map1 = baseCategoryViews.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory1Id));

		Iterator<Map.Entry<Long, List<BaseCategoryView>>> iterator1 = map1.entrySet().iterator();


		while(iterator1.hasNext()){
			JSONObject  category1 =new JSONObject();
			Map.Entry<Long, List<BaseCategoryView>> mapEC1 = iterator1.next();
			List<BaseCategoryView>cg1 = mapEC1.getValue();
			category1.put("categoryId",mapEC1.getKey());
			category1.put("categoryName",cg1.get(0).getCategory1Name());
            //开始分组第二层
			Map<Long, List<BaseCategoryView>> map2 = cg1.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory2Id));
			Iterator<Map.Entry<Long, List<BaseCategoryView>>> iterator2 = map2.entrySet().iterator();
			List<JSONObject> list2 =new ArrayList<>();
			while(iterator2.hasNext()) {
				JSONObject category2 = new JSONObject();
				Map.Entry<Long, List<BaseCategoryView>> mapEC2 = iterator2.next();
				List<BaseCategoryView> cg2 = mapEC2.getValue();

				category2.put("categoryId", mapEC2.getKey());
				category2.put("categoryName", cg2.get(0).getCategory2Name());
//				 开始分组第三层

				Map<Long, List<BaseCategoryView>> map3 = cg2.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory3Id));
				Iterator<Map.Entry<Long, List<BaseCategoryView>>> iterator3 = map3.entrySet().iterator();

				List<JSONObject> list3 =new ArrayList<>();

				while (iterator3.hasNext()) {
					JSONObject category3 = new JSONObject();
					Map.Entry<Long, List<BaseCategoryView>> mapEC3 = iterator3.next();
					List<BaseCategoryView> cg3 = mapEC3.getValue();
					category3.put("categoryId", mapEC3.getKey());
					category3.put("categoryName", cg3.get(0).getCategory3Name());
					list3.add(category3);
				}
				category2.put("categoryChild", list3);
				list2.add(category2);
			}
			category1.put("categoryChild", list2);
			list1.add(category1);
		}
		return list1;
	}
}
